home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / BASICS.ZIP / CLASSLIB / CFRAME.CPP < prev    next >
C/C++ Source or Header  |  1993-05-25  |  29KB  |  1,250 lines

  1. /*
  2.  * CFRAME.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Generic CFrame class that manages either SDI or MDI clients as
  6.  * well as typical File, Edit, Window, and Help commands.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <windows.h>
  19. #include <memory.h>
  20.  
  21. extern "C"
  22.     {
  23.     #include <commdlg.h>
  24.     }
  25.  
  26. #include <win1632.h>
  27. #include <debug.h>
  28. #include "classlib.h"
  29.  
  30.  
  31.  
  32. /*
  33.  * CFrame::CFrame
  34.  * CFrame::~CFrame
  35.  *
  36.  * Constructor Parameters:
  37.  *  hInst           HINSTANCE from WinMain
  38.  *  hInstPrev       HINSTANCE from WinMain
  39.  *  pszCmdLine      LPSTR from WinMain
  40.  *  nCmdShow        int from WinMain
  41.  */
  42.  
  43. CFrame::CFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  44.     , LPSTR pszCmdLine, int nCmdShow)
  45.     : CWindow(hInst)
  46.     {
  47.     m_hInstPrev =hInstPrev;
  48.     m_pszCmdLine=pszCmdLine;
  49.     m_nCmdShow  =nCmdShow;
  50.  
  51.     m_phMenu=NULL;
  52.     m_hBmp=NULL;
  53.     m_pST=NULL;
  54.  
  55.     m_pGB=NULL;
  56.     m_pSS=NULL;
  57.     m_pCL=NULL;
  58.     m_pAdv=NULL;
  59.     return;
  60.     }
  61.  
  62.  
  63.  
  64. CFrame::~CFrame(void)
  65.     {
  66.     m_fClosing=TRUE;
  67.  
  68.     if (NULL!=m_pAdv)
  69.         delete m_pAdv;
  70.  
  71.     //Accelerators freed automatically.
  72.  
  73.     //Free the GizmoBar bitmaps
  74.     if (NULL!=m_hBmp)
  75.         DeleteObject(m_hBmp);
  76.  
  77.     if (NULL!=m_pCL)
  78.         delete m_pCL;
  79.  
  80.     if (NULL!=m_pSS)
  81.         delete m_pSS;
  82.  
  83.     if (NULL!=m_pGB)
  84.         delete m_pGB;
  85.  
  86.     //Free the menu handle array
  87.     if (NULL!=m_phMenu)
  88.         delete []((UINT FAR *)m_phMenu);
  89.  
  90.     //Free the stringtable.
  91.     if (NULL!=m_pST)
  92.         delete m_pST;
  93.  
  94.     m_fClosing=FALSE;
  95.     return;
  96.     }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. /*
  103.  * CFrame::FInit
  104.  *
  105.  * Purpose:
  106.  *  Initializer for a CFrame object containing anything prone to
  107.  *  failure, typically creating the main window and loading resources.
  108.  *
  109.  * Parameters:
  110.  *  pFI             LPFRAMEINIT containing initialization parameters.
  111.  *
  112.  * Return Value:
  113.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  114.  *                  If FALSE is returned, the caller must guarantee that
  115.  *                  the destructor is called promptly to insure cleanup.
  116.  */
  117.  
  118. BOOL CFrame::FInit(LPFRAMEINIT pFI)
  119.     {
  120.     RECT                rc;
  121.     HMENU               hMenu;
  122.     UINT                uTemp;
  123.     TOOLDISPLAYDATA     tdd;
  124.  
  125.     m_fInit=TRUE;
  126.  
  127.     //1.  Create our stringtable
  128.     m_pST=new CStringTable(m_hInst);
  129.  
  130.     if (!m_pST->FInit(pFI->idsMin, pFI->idsMax))
  131.         return FALSE;
  132.  
  133.  
  134.     /*
  135.      * 2.  Register the classes we need for this application.
  136.      *     We have our main (frame) window, document windows (for
  137.      *     either MDI or SDI, and Polyline windows which are the
  138.      *     editing controls.  This separate virtual function allows
  139.      *     applications to add additional classes.
  140.      */
  141.     if (NULL==m_hInstPrev)
  142.         {
  143.         if (!FRegisterAllClasses())
  144.             return FALSE;
  145.         }
  146.  
  147.  
  148.     /*
  149.      * 3.  Create the main window, the gizmobar, the StatStrip,
  150.      *     and a client that owns documents.
  151.      */
  152.     m_pCL=NULL;
  153.  
  154.     m_hWnd=CreateWindow(SZCLASSFRAME, PSZ(IDS_CAPTION)
  155.         , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
  156.         , CW_USEDEFAULT, CW_USEDEFAULT
  157.         , 440, 460, NULL, NULL, m_hInst, (LPVOID)this);
  158.  
  159.     if (NULL==m_hWnd)
  160.         return FALSE;
  161.  
  162.     GetClientRect(m_hWnd, &rc);
  163.  
  164.     UIToolConfigureForDisplay(&tdd);
  165.     m_dxB=tdd.cxButton;
  166.     m_dyB=tdd.cyButton;
  167.     m_cyBar=tdd.cyBar;
  168.  
  169.     m_pGB=new CGizmoBar(m_hInst);
  170.  
  171.     if (!m_pGB->FInit(m_hWnd, ID_GIZMOBAR, m_cyBar))
  172.         return FALSE;
  173.  
  174.  
  175.     m_pSS=new CStatStrip(m_hInst);
  176.  
  177.     if (!m_pSS->FInit(m_hWnd, ID_STATSTRIP, CYSTATSTRIP))
  178.         return FALSE;
  179.  
  180.  
  181.     //Initialize the StatStrip for automated WM_MENUSELECT processing.
  182.     if (!m_pSS->MessageMap(m_hWnd, m_hInst, IDR_STATMESSAGEMAP
  183.         , pFI->idsStatMin, pFI->idsStatMax, CCHMESSAGEMAX
  184.         , pFI->idStatMenuMin, pFI->idStatMenuMax, ID_MESSAGEREADY
  185.         , ID_MESSAGEEMPTY, ID_MENUSYS))
  186.         return FALSE;
  187.  
  188.  
  189.     rc.top+=m_cyBar;
  190.  
  191.     m_pCL=CreateCClient();
  192.  
  193.     /*
  194.      * 4.  Allocate space for the menu handle array and store the popup
  195.      *     handles.  Get the menu handle of the Window menu specifically
  196.      *     for later processing.
  197.      */
  198.  
  199.     hMenu=GetMenu(m_hWnd);
  200.     m_phMenu=new HMENU[pFI->cMenus];
  201.  
  202.     for (uTemp=0; uTemp < pFI->cMenus; uTemp++)
  203.         m_phMenu[uTemp]=GetSubMenu(hMenu, uTemp);
  204.  
  205.     //Save this for UpdateMenus
  206.     m_hMenuWindow=GetSubMenu(hMenu, pFI->iPosWindowMenu);
  207.  
  208.     if (!m_pCL->FInit(m_hMenuWindow, &rc, this))
  209.         return FALSE;
  210.  
  211.  
  212.     /*
  213.      * 5.  Initialize fancy things like the gizmobar.  If a derived class
  214.      *     wants more gizmo images, they can copy the first two in the
  215.      *     standard image set and this code will still load it.  This
  216.      *     code just won't reference it.
  217.      */
  218.  
  219.     m_hBmp=LoadBitmap(m_hInst, MAKEINTRESOURCE(tdd.uIDImages));
  220.  
  221.     if (NULL==m_hBmp)
  222.         return FALSE;
  223.  
  224.     //Create all the gizmos on the gizmobar
  225.     CreateGizmos();
  226.     UpdateGizmos();
  227.  
  228.  
  229.     m_hAccel=LoadAccelerators(m_hInst, MAKEINTRESOURCE(IDR_ACCELERATORS));
  230.  
  231.     if (NULL==m_hAccel)
  232.         return FALSE;
  233.  
  234.  
  235.  
  236.     /*
  237.      * 6.  Before we possibly create any new documents, create an
  238.      *     AdviseSink for document notifications.
  239.      */
  240.  
  241.     m_pAdv=new CDocumentAdviseSink((LPVOID)this);
  242.  
  243.  
  244.     /*
  245.      * 7.  In the default implementation FPreShowInit does not do
  246.      *     anything, but is called here to allow derivations to
  247.      *     hook the function and modify m_nCmdShow before we
  248.      *     call ShowWindow.  This is one such place where OLE affects
  249.      *     a derivation, because servers will change m_nCmdShow to
  250.      *     SW_HIDE if started with -Embedding.
  251.      */
  252.     if (!FPreShowInit())
  253.         return FALSE;
  254.  
  255.  
  256.     /*
  257.      * 8.  Handle window visibility appropriately after giving
  258.      *     FPreShowInit a chance to modify it.
  259.      */
  260.     ShowWindow(m_hWnd, m_nCmdShow);
  261.     UpdateWindow(m_hWnd);
  262.  
  263.  
  264.     /*
  265.      * 9.  Parse the command line and take appropriate action.  Again,
  266.      *     this virtual function is here for applications to override.
  267.      */
  268.     ParseCommandLine();
  269.  
  270.     m_fInit=FALSE;
  271.     return TRUE;
  272.     }
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. /*
  280.  * CFrame::CreateCClient
  281.  *
  282.  * Purpose:
  283.  *  Creates a CClient object for use in this frame.  This function is
  284.  *  overrided by derived classes to create a different type of CClient.
  285.  *
  286.  * Parameters:
  287.  *  None
  288.  *
  289.  * Return Value:
  290.  *  LPCClient        Pointer to the new CClient object.
  291.  */
  292.  
  293. LPCClient CFrame::CreateCClient()
  294.     {
  295.     return new CClient(m_hInst);
  296.     }
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304. /*
  305.  * CFrame::FRegisterAllClasses
  306.  *
  307.  * Purpose:
  308.  *  Registers all classes used in this application.
  309.  *
  310.  * Parameters:
  311.  *  None
  312.  *
  313.  * Return Value:
  314.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  315.  */
  316.  
  317. BOOL CFrame::FRegisterAllClasses(void)
  318.     {
  319.     WNDCLASS        wc;
  320.  
  321.     //Field that are the same for all windows.
  322.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  323.     wc.hInstance     = m_hInst;
  324.     wc.cbClsExtra    = 0;
  325.  
  326.     //Register the Frame window
  327.     wc.lpfnWndProc   = FrameWndProc;
  328.     wc.cbWndExtra    = CBFRAMEWNDEXTRA;
  329.     wc.hIcon         = LoadIcon(m_hInst, "Icon");
  330.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  331.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  332.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  333.     wc.lpszClassName = SZCLASSFRAME;
  334.  
  335.     if (!RegisterClass(&wc))
  336.         return FALSE;
  337.  
  338.    #ifndef MDI
  339.     wc.lpfnWndProc   = SDIClientWndProc;
  340.     wc.cbWndExtra    = CBCLIENTWNDEXTRA;
  341.     wc.hIcon         = NULL;
  342.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  343.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  344.     wc.lpszMenuName  = NULL;
  345.     wc.lpszClassName = SZCLASSSDICLIENT;
  346.  
  347.     if (!Regi